package com.wanghlme.bookstore.controller.admin;

import com.alibaba.fastjson.JSON;
import com.wanghlme.bookstore.entity.Category;
import com.wanghlme.bookstore.service.CategoryService;
import com.wanghlme.bookstore.util.common.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wanghlme.bookstore.util.OperationStatusEnum.*;

@Slf4j
@RequestMapping("/admin/category")
@Controller("AdminCategoryController")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/toInsertUI")
    public String toInsertUI() {
        log.info(Thread.currentThread().getStackTrace()[1].getMethodName());
        return "admin/category/add";
    }

    @RequestMapping("/toUpdateUI")
    public String toUpdateUI() {
        log.info(Thread.currentThread().getStackTrace()[1].getMethodName());
        return "admin/category/list";
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public String insert(@RequestBody Category category) {
        if (category == null) {
            return NULL.getValue();
        }
        log.debug("CurrentCategory: {}", category.toString());
        return categoryService.save(category) ? SUCCESS.getValue() : FAILURE.getValue();
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    @ResponseBody
    public String listAll() {
        List<Category> list = categoryService.list();
        return JSON.toJSONString(list);
    }

    @RequestMapping(value = "/delete?id={delId}", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("delId") String delId) {
        if (StringUtil.isNullEmptyBlank(delId)) {
            return NULL.getValue();
        }
        log.debug("CurrentID: {}", delId);
        return categoryService.removeById(delId) ? SUCCESS.getValue() : FAILURE.getValue();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody Category category) {
        if (category == null) {
            return NULL.getValue();
        }
        log.debug("CurrentCategory: {}", category.toString());
        return categoryService.updateById(category) ? SUCCESS.getValue() : FAILURE.getValue();
    }
    /**
     *
     * public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     *
     * 		Category category = WebUtils.request2Bean(request, Category.class);
     *
     * 		boolean success = categoryService.addCategory(category);
     *
     * 		if(success) {
     * 			WebUtils.forwordMessageUI(request, response, "添加分类成功!");
     *                } else {
     * 			WebUtils.forword(request, response, "/manager/category/add.jsp", "message", "此分类已存在!");
     *        }* 	}
     *
     * 	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     * 		List<Category> list = categoryService.getAllCategorys();
     *
     * 		WebUtils.forword(request, response, "/manager/category/list.jsp", "list", list);
     *    }
     *
     *
     * 	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     * 		System.out.println("--delete()--");
     *
     * 		String id = request.getParameter("categoryid");
     * 		System.out.println(id);
     * 		categoryService.deleteCategoryById(id);
     * 		System.out.println("已获取id");
     * 		WebUtils.forwordMessageUI(request, response, "删除分类成功!");
     *    }
     *
     * 	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     * 		System.out.println("---CategoryServlet update()---");
     *
     * 		Category category = WebUtils.request2Bean(request, Category.class);
     * 		categoryService.updateCategory(category);
     * 		WebUtils.forwordMessageUI(request, response, "更新分类成功!");
     *    }
     */
}
